<?php
/**
 * Shop_Model_AttrsSet
 * 
 * Jaki zestaw attrybutów ma jak jakie attrybuty
 * 
 * @version $Id$
 * @author $Author$
 */
class Shop_Model_AttrsSet extends Promotor_Model_Scaffold {

	protected $_dbTableClass = 'Shop_Model_DbTable_AttrsSet';
	
	protected $_cachedMethods = array(
		'getSetById',
	);

	public function selectList() {
		$db = $this->getDbTable()
				   ->getAdapter();

		$select = new Zend_Db_Select($db);

		$select->from(array('s' => 'shop_product_attrs_set'))
			   ->join(
			   		array('pa' => 'shop_product_attr'),
			   			'pa.id = s.attr_id', array('attr_name' => 'name'))
			   	->join(
			   		array('ps' => 'shop_product_attr_set'),
			   			'ps.id = s.attr_set_id', array('attr_set_name' => 'name'));

		return $select;
	}

	/**
	 * Pobieranie zestawu atrybutów,
	 * 
	 * Gdy zostanie podany parametr $productId
	 * wyszukiwane są wartości danego attrybutu dla produktu
	 * 
	 * @param integer id
	 * @param integer|array $productId
	 * @return array|null
	 */
	public function getSetById($id, $productId = null) {
		$db = $this->getDbTable()
				   ->getAdapter();

		$select = new Zend_Db_Select($db);

		$select->from(array('s' => 'shop_product_attrs_set'), array('attr_id'))
			   ->join(
			   		array('pa' => 'shop_product_attr'),
			   			'pa.id = s.attr_id', array('name','type','required','default','description'))

			   	->join(
			   		array('ps' => 'shop_product_attr_set'),
			   			'ps.id = s.attr_set_id', array('set_name' => 'name'))

			   	->where('ps.id = ?', $id, Zend_Db::INT_TYPE);

		// Dodaj wartości produktu jeżeli istnieją
		if (is_array($productId)
				&& isset($productId['id']))
		{
			$productId = $productId['id'];
		}

		if (is_numeric($productId)) {
			$where = $db->quoteInto(
				'pv.attr_id = s.attr_id AND pv.product_id = ?', $productId, Zend_Db::INT_TYPE);

			$select->joinLeft(
			   		array('pv' => 'shop_product_attr_val'),
			   		$where,
			   		array('value'));
		}
			   	
		try {
			$stmt = $select->query();
		} catch(Zend_Db_Exception $e) {
			$this->_setStatus(self::FAILURE);
			$this->_addException($e);
			return;
		}

		return $stmt->fetchAll();
	}
	
	/**
	 * @param integer $attrSetId
	 * @param array $attribsValues
	 * @return void
	 */
	public function attacheAttribsToAttrSet($attrSetId, array $attribsValues) {
		$table = $this->getDbTable();
		$db = $table->getAdapter();
		
		$db->beginTransaction();

		$table->delete($db->quoteInto('attr_set_id = ?', $attrSetId));

		try {
			foreach ($attribsValues as $attrId)
			{
				if (!is_numeric($attrId))
				{
					continue;
				}
				
				$data = array(
					'attr_set_id' => $attrSetId,
					'attr_id' => $attrId
				);

				$table->insert($data);
			}

			$db->commit();
			$this->_setStatus(self::SUCCESS);
		} catch (Zend_Db_Exception $e) {
			$db->rollBack();
			$this->_addException($e);
			$this->_setStatus(self::FAILURE);
		}
	}
	
	public function findAttrForSet($setId) {
		$values = array();

		if(is_array($setId))
		{
			$setId = $setId['id'];
		}

		if (is_numeric($setId))
		{
			$table = $this->getDbTable();
			$rowset = $table->fetchAll(
					$table->select()->where('attr_set_id = ?', $setId['id']));
	
			foreach ($rowset as $row)
			{
				$values[] = $row->attr_id;
			}
		}

		return $values;
	}
}